import {createRouter,createWebHistory} from "vue-router";
import {useRouterStore} from "../store/useRouterStore.js";
import { storeToRefs } from "pinia";
import Home from '../views/home/Home.vue';
import Notfound from '../components/Notfound.vue';
import Login from '../components/Login.vue';
// import Study from '../views/study/Study.vue'

const routes=[
    // 重定向，在加载页面时就进入index页面
    {
        path:"/",
        redirect:"/home"
    },
    // 输入错误路径跳转至Notfound
    {
        path:"/:lk(.*)*",
        component: Notfound
    },
    // 登录成功后
    {
        path:"/mainbox",
        component:()=>import('../views/MainBox.vue'),
        children:[
            // 首页
    {
        path:"/home",
        name:"Home",
        component:Home
    },
    // 学习
    {
        path: '/study',
        alias:"/xuexi",// 别名
        name:"study",// 命名路由
        // component: Study,
        children: [
            // 汉诺塔问题
            {
                path: "recurhanoi",
                component: ()=>import('../views/study/recur-hanoi/recursionHanoiTower.vue'),
            },
            // 排序问题
            {
                path:"/study/sort",
                children: [
                    // 冒泡排序
                    {
                        path: "/study/sort/bubsort",
                        component:()=>import('../views/study/sort/bub-sort/bubblingSort.vue')
                    },
                    // 归并排序
                    {
                        path: "/study/sort/mergersort",
                        component:()=>import('../views/study/sort/merge-sort/mergerSort.vue')
                    },
                    // 快速排序
                    {
                        path: "/study/sort/fastsort",
                        component:()=>import('../views/study/sort/fast-sort/fastSort.vue')
                    },
                ]
            },
            // 动态规划
            {
                path:"/study/dp",
                children: [
                    // 背包问题
                    {
                        path: "/study/dp/knapro",
                        component:()=>import('../views/study/dynamic-programming/kna-pro/KnaPro.vue')
                    },
                    // 最长公共子序列
                    {
                        path: "/study/dp/longcomsub",
                        component:()=>import('../views/study/dynamic-programming/long-com-sub/longComSub.vue')
                    },
                ]
            },
            // 贪心算法
            {
                path:"/study/greedy",
                children: [
                    // 分配问题
                    {
                        path: "/study/greedy/asspro",
                        component:()=>import('../views/study/greedy-algorithms/ass-pro/assPro.vue')
                    },
                    // 集合覆盖问题
                    {
                        path: "/study/greedy/setcovpro",
                        component:()=>import('../views/study/greedy-algorithms/setcov-pro/setCovPro.vue')
                    },
                ]
            },
            // 搜索算法
            {
                path:"/study/search",
                children: [
                    // 二分查找
                    {
                        path: "/study/search/binarysearch",
                        component:()=>import('../views/study/search-algorithms/binary-search/binarySearch.vue')
                    },
                    // 插值查找
                    {
                        path: "/study/search/InterpolationSearch",
                        component:()=>import('../views/study/search-algorithms/interpolation-search/InterpolationSearch.vue')
                    },
                ]
            },
            // 图算法
            {
                path: "/study/graph",
                children: [
                    // 最短路径-Dijkstra算法
                    {
                        path: "/study/graph/shortest-path/dijkstra",
                        component:()=>import('../views/study/graph-algorithms/shortest-path/dijkstra/Dijkstra.vue')
                    },
                      // 最短路径-Floyd算法
                      {
                        path: "/study/graph/shortest-path/floyd",
                        component:()=>import('../views/study/graph-algorithms/shortest-path/floyd/Floyd.vue')
                    },
                    // 最小生成树-Kruskal算法
                    {
                        path: "/study/graph/min-span-tree/kruskal",
                        component:()=>import('../views/study/graph-algorithms/min-span-tree/kruskal/Kruskal.vue')
                    },
                    // 最小生成树-Prim算法
                    {
                        path: "/study/graph/min-span-tree/prim",
                        component:()=>import('../views/study/graph-algorithms/min-span-tree/prim/Prim.vue')
                    },
                ]
            },
            // 汉诺塔问题
            {
                path: "n-queen-question",
                component: ()=>import('../views/study/n-queen-question/NQQ.vue'),
            },
        ]
    },
    // 交流模块
    {
        path: "/comment",
        component: ()=>import('../views/comment/Comment.vue')
    },
    // 个人模块
    {
        path: "/center",
        component: ()=>import('../views/center/Center.vue'),
        // meta:{
        //     requiredAuth:true// 添加路由拦截
        // }
    },
      // 管理员权限
    // 用户中心
    {
        path: "/users",
        component: ()=>import('../views/user/UserInfo.vue'),
    },
    // 审批中心
    {
        path:"/approval",
        children:[
            {
                path:"users",
                component: ()=>import('../views/approval/UserInfoApproval.vue'),
            },
            {
                path:"comments",
                component: ()=>import('../views/approval/CommentApproval.vue'),
            },
        ]
    },

        ]
    },
    
    // {// 动态路由
    //     path: "/detail/:myid",// :为占位符，后面的名字随意
    //     component: Detail,
    //     name:"Detail"
    // },
    // 登录
    {
      path: "/login",
      name:"Login",
      component: Login
    },
    
]
const router=createRouter({
    history:createWebHistory(),
    routes,
})

//路由拦截：
// router.beforeEach(async (to,from,next)=>{
//     let isAuthenticated=await localStorage.getItem("taken")
//     if(to.name!=='Login'&&!isAuthenticated&&to.meta.requiredAuth)next({name:'Login'})
//     else next()
// })
router.beforeEach( (to,from,next)=>{
    const routerStore=useRouterStore();
    const {isGetterRouter}=storeToRefs(routerStore);
    if(to.name==="Login"||to.name==="Home"){
       next()
    }
    else{   
            if(!isGetterRouter.value){
                router.push("/login");
            }else {
                next()
            }
        }
})

export default router